package com.s7.compat.manager;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Process;

import java.util.Stack;

public class AppManager {

    private static Stack<Activity> activityStack;
    private static AppManager instance;

    public static AppManager getInstance() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    public Stack<Activity> getActivityStack() {
        return activityStack;
    }

    public void add(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }
        addExist(activity);
        activityStack.add(activity);
    }

    public void addExist(Activity activity) {
        Activity exist = null;
        for (Activity stored : activityStack) {
            if (stored.getClass().equals(activity.getClass())) {
                exist = stored;
            }
        }
        finish(exist);
    }

    public Activity current() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    public void remove() {
        Activity activity = activityStack.lastElement();
        remove(activity);
    }

    public void remove(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
        }
    }

    public void remove(Class<?> cls) {
        Activity activity = null;
        for (Activity stored : activityStack) {
            if (stored.getClass().equals(cls)) {
                activity = stored;
            }
        }
        remove(activity);
    }

    public int size() {
        if (activityStack != null) {
            return activityStack.size();
        }
        return 0;
    }

    public void finish() {
        Activity activity = activityStack.lastElement();
        finish(activity);
    }

    public void finish(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
        }
    }

    public void finish(Class<?> cls) {
        Activity activity = null;
        for (Activity stored : activityStack) {
            if (stored.getClass().equals(cls)) {
                activity = stored;
            }
        }
        finish(activity);
    }

    public void finishTest(Class<?> cls) {
        for (Activity stored : activityStack) {
            if (stored.getClass().equals(cls)) {
                finish(stored);
                break;
            }
        }
    }

    public void finishAll() {
        for (int i = 0; i < activityStack.size(); i++) {
            if (activityStack.get(i) != null) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    @SuppressWarnings("deprecation")
    public void AppExit(Context context) {
        if (activityStack != null) {
            finishAll();
        }
        try {
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityManager.restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("deprecation")
    public void AppException(Context context) {
        if (activityStack != null) {
            finishAll();
        }
        try {
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityManager.restartPackage(context.getPackageName());
            Process.killProcess(Process.myPid());
            System.exit(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
